/**
 * Created by Elsa on 2017/8/30.
 */
layui.define(['form', 'layedit', 'laydate','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer,
        layedit = layui.layedit,
        laydate = layui.laydate;

    var inspnum = getQueryString("inspnum");
    console.log(tampdata);

    form.on('select(type)', function(data){
        if(data.value == '通过'){
            $('#rectnumlabel').html("");
            $('input[name="rectnum"]').hide();
            $('#norect').show();
            $('#rect').hide();
            $('#rectt').hide();
            form.render();
        } else{
            $('#rectnumlabel').html("整改编号");
            $('input[name="rectnum"]').show();
            $('#norect').hide();
            $('#rect').show();
            form.render();
        }
    });

    form.render();

    //自定义验证规则
    form.verify({
        title: function(value) {
            if(value.length < 5) {
                return '标题至少得5个字符啊';
            }
        },
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        content: function(value) {
            layedit.sync(editIndex);
        }
    });

    //监听提交
    form.on('submit(caseglyx)', function(data) {
        $(this).attr('disabled','disabled');
        var ConstYcsReview = new Object();
        var ConstYcsReviewCc = new Object();
        var ConstYcsReviewCd = new Object();

        ConstYcsReview.inspnum = inspnum;
        ConstYcsReview.inspdate = tampdata;
        ConstYcsReview.inspopinion = $('select[name="inspopinion"] option:selected').val();
        ConstYcsReview.opncontent = data.field.opncontent;
        ConstYcsReview.rectopinion = data.field.rectopinion;
        ConstYcsReview.rectresult = data.field.rectresult;
        ConstYcsReview.rectnum = data.field.rectnum;
        ConstYcsReview.ycs2 = data.field.ycs2;
        ConstYcsReview.rq4 = data.field.rq4;
        ConstYcsReview.confirmresult = $('select[name="confirmresult"] option:selected').val();
        ConstYcsReview.ycs3 = data.field.ycs3;

        ConstYcsReviewCc.inspnum = inspnum;
        ConstYcsReviewCc.inspdate = tampdata;
        ConstYcsReviewCc.ccopn = $('select[name="ccopn"] option:selected').val();

        ConstYcsReviewCd.inspnum = inspnum;
        ConstYcsReviewCd.inspdate = tampdata;
        ConstYcsReviewCd.cdopn = $('select[name="cdopn"] option:selected').val();

        $.ajax({
            type:"GET",
            url:contextPath+"/personal/role",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if(data.role=="验船师"){
                    console.log(JSON.stringify(ConstYcsReview));

                    $.ajax({
                        type:"PUT",
                        url:contextPath+"/project/const/inspdclr/update/ycscheck",
                        contentType: "application/json; charset=utf-8",
                        data:JSON.stringify(ConstYcsReview),
                        dataType:"json",
                        success:function (data) {
                            console.log(data.message);
                            if(data.message == "更新成功!"){
                                layer.msg('建造检验报检单已审核!<br>页面将在1秒后刷新...');
                                setTimeout(function(){window.location.reload();},1000);
                            } else {
                                $('#suresubmitx').attr('disabled',false);
                                layer.msg(data.message, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                            }
                        },
                        error:function (e) {
                        }
                    });
                }else if(data.role=="船厂"){
                    console.log(JSON.stringify(ConstYcsReviewCc));

                    $.ajax({
                        type:"PUT",
                        url:contextPath+"/project/const/inspdclr/update/idccopn",
                        contentType: "application/json; charset=utf-8",
                        data:JSON.stringify(ConstYcsReviewCc),
                        dataType:"json",
                        success:function (data) {
                            console.log(data.message);
                            if(data.message == "更新成功!"){
                                layer.msg('建造检验报检单已审核!<br>页面将在1秒后刷新...');
                                setTimeout(function(){window.location.reload();},1000);
                            } else {
                                $('#suresubmitx').attr('disabled',false);
                                layer.msg(data.message, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                            }
                        },
                        error:function (e) {
                        }
                    });

                }else if(data.role=="船东"){
                    console.log(JSON.stringify(ConstYcsReviewCd));

                    $.ajax({
                        type:"PUT",
                        url:contextPath+"/project/const/inspdclr/update/idcdopn",
                        contentType: "application/json; charset=utf-8",
                        data:JSON.stringify(ConstYcsReviewCd),
                        dataType:"json",
                        success:function (data) {
                            console.log(data.message);
                            if(data.message == "更新成功!"){
                                layer.msg('建造检验报检单已审核!<br>页面将在1秒后刷新...');
                                setTimeout(function(){window.location.reload();},1000);
                            } else {
                                $('#suresubmitx').attr('disabled',false);
                                layer.msg(data.message, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                            }
                        },
                        error:function (e) {
                        }
                    });

                }else{

                }
            },
            error: function (e) {

            }
        });

        return false;
    });

    exports('ConstYcsReview', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});